package com.jmx.yp.service.member;

import java.util.*;

import com.jmx.yp.common.SysConstants;
import com.jmx.yp.dao.IBaseDao;
import com.jmx.yp.entity.EmpGrade;
import com.jmx.yp.exception.ErrorCode;
import com.jmx.yp.exception.HandlerException;
import com.jmx.yp.util.Tools;

/**
 * 
 * 功能描述：
 *			接口IGradeService实现类
 * @author Jenny
 *
 * Date:2011-2-16 下午05:53:23
 */
public class GradeService implements IGradeService {

	private IBaseDao dao;
	
	public void setDao(IBaseDao dao){
		this.dao = dao;
	}
	
	/**
	 * 新增会员级别
	 * @param grade
	 * @throws Exception
	 */
	public void addGrade(EmpGrade grade) throws Exception{
		if(isSameNameInCategory(grade.getName(), grade.getCategoryId(), null))
			throw new HandlerException(ErrorCode.ERR_4002);
		
		dao.saveObject("com.jmx.yp.entity.EmpGradeMapper.addGrade", grade);
	}
	
	/**
	 * 判断在同一会员类别中是否有相同名称
	 * @param gradeName
	 * @param categoryId
	 * @param serialid
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	private boolean isSameNameInCategory(String gradeName, Long categoryId, Long serialid) throws Exception{
		boolean flag = false;
		
		Map paramMap = new HashMap();
		paramMap.put("gradeName", gradeName.trim());
		paramMap.put("categoryId", categoryId);
		if(serialid != null)
			paramMap.put("serialid", serialid);
		int count = (Integer)dao.getObject("com.jmx.yp.entity.EmpGradeMapper.isSameNameInCategory", paramMap);
		if(count > 0)
			flag = true;
		
		return flag;
	}
	
	/**
	 * 根据会员级别名称及所属类别查询会员级别
	 * @param queryName
	 * @param queryCategoryId
	 * @param nowPage
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public Map queryGrade(String queryName, Long queryCategoryId, int nowPage) throws Exception{
		Map map = new HashMap(2);
		int pageSize = dao.getPageSize();
		
		//参数MAP
		Map paramMap = new HashMap();
		if(queryName != null && queryName.trim().length() > 0)
			paramMap.put("queryName", "%" + queryName.trim() + "%");
		if(queryCategoryId != null && queryCategoryId > 0)
			paramMap.put("queryCategoryId", queryCategoryId);
		
		//总数
		int total = (Integer)dao.getObject("com.jmx.yp.entity.EmpGradeMapper.countQueryGrade", paramMap);
		
		paramMap.put("pageSize", pageSize);
		paramMap.put("page", (nowPage - 1) * pageSize);
		
		//LIST
		List list = dao.getList("com.jmx.yp.entity.EmpGradeMapper.queryGrade", paramMap);
		if(nowPage > 1 && (list == null || list.isEmpty())){ //当前页>1，查询又无结果，说明是以前的页数已经被删完。则自动显示最后一页
			nowPage = Tools.computeTotalPage(total, pageSize);
			paramMap.put("page", (nowPage - 1) * pageSize);
			list = dao.getList("com.jmx.yp.entity.EmpGradeMapper.queryGrade", paramMap);
		}
		
		//handleListTel(list);
		
		map.put("total", total); //总记录数
		map.put("rows", list);
		map.put("nowPage", nowPage); //当前页
		map.put("pageTotal", Tools.computeTotalPage(total, pageSize));                    //总页数
		
		return map;
	}
	
	/**
	 * 根据ID查询会员级别对象
	 * @param serialid
	 * @return
	 * @throws Exception
	 */
	public EmpGrade getGrade(Long serialid) throws Exception{
		EmpGrade grade = (EmpGrade)dao.getObject("com.jmx.yp.entity.EmpGradeMapper.getGrade", serialid);
		return grade;
	}
	
	/**
	 * 修改会员级别
	 * @param grade
	 * @throws Exception 当同一类别中有相同会员级别名称时抛出异常
	 * 备注：暂未考虑当有该类会员时，不允许修改的属性
	 */
	public void updateGrade(EmpGrade grade) throws Exception{
		if(isSameNameInCategory(grade.getName(), grade.getCategoryId(), grade.getSerialid()))
			throw new HandlerException(ErrorCode.ERR_4002);
		
		dao.updateObject("com.jmx.yp.entity.EmpGradeMapper.updateGrade", grade);
	}
	
	/**
	 * 删除会员级别
	 * @param gradeIds
	 * @param operator
	 * @throws Exception 当有会员已关联会员级别时，抛异常
	 */
	@SuppressWarnings("unchecked")
	public void deleteGrade(String gradeIds, String operator) throws Exception{
		if(gradeIds == null || gradeIds.length()<= 0)
			return;
		if(isRelationMembers(gradeIds))
			throw new HandlerException(ErrorCode.ERR_4003);
		
		Map paramMap = new HashMap(4);
		paramMap.put("array", gradeIds.split(";"));
		paramMap.put("operator", operator);
		paramMap.put("operateTime", new Date());
		paramMap.put("invalid", SysConstants.INVALID);
		
		dao.updateObject("com.jmx.yp.entity.EmpGradeMapper.deleteGrade", paramMap);
	}
	
	/**
	 * 会员级别是否关联会员
	 * @param gradeIds
	 * @return
	 * @throws Exception
	 */
	private boolean isRelationMembers(String gradeIds) throws Exception{
		boolean flag = false;
		
		int count = (Integer)dao.getObject("com.jmx.yp.entity.EmpGradeMapper.isRelationMembers", gradeIds.split(";"));
		if(count > 0)
			flag = true;
		
		return flag;
	}
}
